#!/usr/bin/env python
# coding: utf-8

# In[1]:


from xml.dom import minidom
import socket
import tkinter as tk
from tkinter import ttk
import xml
import threading
import time
import socket
import json
import queue
import pandas as pd
queue1 = queue.Queue()
queue = queue.Queue()
def server_start():
    ip_port = (socket.gethostname(), 50000)
    s = socket.socket()     # 创建套接字
    s.connect(ip_port)    # 连接服务器
    while True:
        inp = queue.get()
        if inp == "exit":   # 如果输入的是‘exit’，表示断开连接
            s.sendall(inp.encode())
            server_reply = s.recv(1024).decode()
            print("You have exit. %s"%server_reply)
            break
        else:
            s.sendall(inp.encode())
            server_reply = s.recv(1024).decode()
            server_reply = json.loads(server_reply)
            queue1.put(server_reply)
    s.close()
def plotxy():
    if queue1.empty():
        print('请重新发送一遍！！！')
    else:
        data = queue1.get()
        df = pd.DataFrame(data)
        df.year = pd.to_datetime(df.year)
        df.value = pd.to_numeric(df.value)
        df.set_index(df.year,inplace=True)
        df.value.plot()
#open xml file
dom = xml.dom.minidom.parse('UNData.xml')

#get elements in the file
root = dom.documentElement

#search by Tag
cc = dom.getElementsByTagName('Country')

for c in cc:
    # elements of country
    country = []
    a = 0
    while a < len(cc):
        country.append(cc[a].firstChild.data)
        a+=28

# use Tkinter , create a window

t = threading.Thread(target=server_start)
t.start()
win = tk.Tk()
win.title("Select a country")  # Title of the window
win.geometry("500x300+200+20")
'''
pull-down menu
'''
xVariable = tk.StringVar()

# Create a pull-down menu

com = ttk.Combobox(win, textvariable=xVariable)
com.pack()
com["value"] = country
com.current(0)  # Set a default value
com.bind("<<ComboboxSelected>>")
button1 = tk.Button(win, text="plotxy",command=plotxy)
def put_info():
    country_select = com.get()
    queue.put(country_select)
    button1.pack()
button = tk.Button(win, text="send",command=put_info)
button.pack()
win.mainloop()


# In[ ]:




